package com.bukkit.gauss.Pseudocide;

import org.bukkit.ChatColor;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.event.Event;
import org.bukkit.plugin.Plugin;
import org.bukkit.plugin.PluginDescriptionFile;
import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.plugin.PluginManager;
import java.util.logging.Logger;

import com.nijiko.permissions.PermissionHandler;
import com.nijikokun.bukkit.Permissions.Permissions;

/*
 * Pseudocide Version 1.2 - Easy inventory cleaner
 * Copyright (C) 2011 Gauss Software
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

/**
 * Main pseudocide class
 * 
 * @author gauss
 * 
 */
public class Pseudocide extends JavaPlugin{
	
	/**
	 * Block listener, activated when a block is right-clicked or a sign is changed
	 */
    private final PseudocideBlockListener blockListener = new PseudocideBlockListener(this);
    
    /**
     * Logger magic
     */
    public static final Logger log = Logger.getLogger("Minecraft");
	
    /**
     * Permission plugin
     */
    public static PermissionHandler Permissions = null;

	/**
	 * Outputs a message when disabled
	 */
	public void onDisable() {
		PluginDescriptionFile pdfFile = this.getDescription();
		log.info(pdfFile.getName() + " version " + pdfFile.getVersion() + "disabled.");
		
	}
	
	/**
	 * Makes sure that the Permissions plugin is installed, disabling the plugin if not
	 */
	public void setupPermissions() {
		Plugin test = this.getServer().getPluginManager().getPlugin("Permissions");
		PluginDescriptionFile pdfFile = this.getDescription();
		
		if (Pseudocide.Permissions == null) {
			if (test!= null) {
				this.getServer().getPluginManager().enablePlugin(test);
				Pseudocide.Permissions = ((Permissions) test).getHandler();
			}
			else {
				log.info(pdfFile.getName() + " version " + pdfFile.getVersion() + "not enabled. Permissions not detected");
				this.getServer().getPluginManager().disablePlugin(this);
			}
		}
	}
	
	/**
	 * Setup Permissions plugin
	 * Hook the events into the plugin manager
	 */
	public void onEnable() {
		
		setupPermissions();
		
		PluginManager pm = getServer().getPluginManager();
		
		pm.registerEvent(Event.Type.SIGN_CHANGE, this.blockListener, Event.Priority.Normal, this);
		pm.registerEvent(Event.Type.BLOCK_RIGHTCLICKED, this.blockListener, Event.Priority.Normal, this);
		
		//Get the infomation from the yml file.
        PluginDescriptionFile pdfFile = this.getDescription();
        //Print that the plugin has been enabled!
        System.out.println( pdfFile.getName() + " version " + pdfFile.getVersion() + " is enabled!" );        
	}
	
	@Override
    public boolean onCommand(CommandSender sender, Command command, String commandLabel, String[] args) {
        String[] split = args;
        String commandName = command.getName().toLowerCase();
        
        if (sender instanceof Player) {
            Player player = (Player) sender;
            
            if (commandName.equals("pseudocide")) {
                /**
                 * /pseudocide
                 */
                if (split.length == 0 && Permissions.has(player, "pseudocide.use") ) {
                    
                	Pseudocide.clearInventory( player );
    				return true;
                 
                } 
                else if (split.length == 1 && split[0].equalsIgnoreCase("all") && Permissions.has(player, "pseudocide.use") ) {
                    
                	Pseudocide.clearInventoryAndHotbar( player );
    				return true;
    				
                }
                else if (split.length == 1 && split[0].equalsIgnoreCase("help") && Permissions.has(player, "pseudocide.use") ) {
                    
                	player.sendMessage(ChatColor.RED + "/pseudocide" + ChatColor.WHITE + "  -  Clear the inventory");
                	player.sendMessage(ChatColor.RED + "/pseudocide all" + ChatColor.WHITE + "  -  Clear the inventory and hotbar");
                } else {
                    return false;
                }
                return true;
            }
        }
        return false;
    }
	
	public static void clearInventory( Player player ) {
		//Clears the inventory sans the 9 active ones
		for( int i = 9; i <= 35; i++ ) {
			player.getInventory().clear(i);
		}
		player.updateInventory();
		//Deprecated, but no other good way to do it.
		
		return;
	}
	
	public static void clearInventoryAndHotbar( Player player ) {
		//Clears the inventory
		player.getInventory().clear();
		player.updateInventory();
		//Deprecated, but no other good way to do it.
		
		return;
	}
	
}